import { message } from 'antd';
export default {
  namespace: 'hdst',
  state: {
    offlineZmcParams: [], // 离线上料控制器的参数
    camZmcParams: [], // 拍照控制器参数
    cbjZmcParams: [], // 裁切机控制器参数
    cutterZmcParams: [], // 铡刀控制器参数
    lineCamCheckThres: [], // 上料线阵相机计算用阈值参数
    cutterCamCheckThres: [], // 铡刀面阵相机计算用阈值参数
    configParams: [], // 设备设置参数,包括相机还有其他的一些参数
  },
  effects: {
    *handleSettingsUpdate({ payload }, { call, put }) {
      yield put({
        type: 'getOfflineZmcParams',
      });
      yield put({
        type: 'getCamZmcParams',
      });
      yield put({
        type: 'getCbjZmcParams',
      });
      yield put({
        type: 'getCutterZmcParams',
      });
      yield put({
        type: 'getLineCamCheckThres',
      });
      yield put({
        type: 'getCutterCamCheckThres',
      });
    },
    *getOfflineZmcParams({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'offlineZmc',
        'web_request_get_offline_zmc_params',
        []
      );
      yield put({
        type: 'updateOfflineZmcParams',
        payload: response,
      });
    },
    *saveOfflineZmcParams({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'offlineZmc',
        'web_request_save_offline_zmc_params',
        [payload]
      );
      if (response === 'ok') {
        message.success('保存成功!');
      } else {
        message.error('保存失败,', response);
      }
      yield put({
        type: 'getOfflineZmcParams',
      });
    },

    *getCamZmcParams({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'camZmc',
        'web_request_get_cam_zmc_params',
        []
      );
      yield put({
        type: 'updateCamZmcParams',
        payload: response,
      });
    },
    *saveCamZmcParams({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'camZmc', 'web_request_save_cam_zmc_params', [
        payload,
      ]);
      if (response === 'ok') {
        message.success('保存成功!');
      } else {
        message.error('保存失败,', response);
      }
      yield put({
        type: 'getCamZmcParams',
      });
    },
    *getCbjZmcParams({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'cbjZmc',
        'web_request_get_cbj_zmc_params',
        []
      );
      yield put({
        type: 'updateCbjZmcParams',
        payload: response,
      });
    },
    *saveCbjZmcParams({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'cbjZmc', 'web_request_save_cbj_zmc_params', [
        payload,
      ]);
      if (response === 'ok') {
        message.success('保存成功!');
      } else {
        message.error('保存失败,', response);
      }
      yield put({
        type: 'getCbjZmcParams',
      });
    },
    *getCutterZmcParams({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'cutterZmc',
        'web_request_get_cutter_zmc_params',
        []
      );
      yield put({
        type: 'updateCutterZmcParams',
        payload: response,
      });
    },
    *saveCutterZmcParams({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'cutterZmc',
        'web_request_save_cutter_zmc_params',
        [payload]
      );
      if (response === 'ok') {
        message.success('保存成功!');
      } else {
        message.error('保存失败,', response);
      }
      yield put({
        type: 'getCutterZmcParams',
      });
    },
    *getLineCamCheckThres({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'ph',
        'web_request_get_line_cam_check_thres',
        []
      );
      yield put({
        type: 'updateLineCamCheckThres',
        payload: response,
      });
    },
    *saveLineCamCheckThres({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'ph',
        'web_request_save_line_cam_check_thres',
        [payload]
      );
      if (response === 'ok') {
        message.success('保存成功!');
      } else {
        message.error('保存失败,'+ response, 5);
      }
      yield put({
        type: 'getLineCamCheckThres',
      });
    },
    *getCutterCamCheckThres({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'ph',
        'web_request_get_cutter_cam_check_thres',
        []
      );
      yield put({
        type: 'updateCutterCamCheckThres',
        payload: response,
      });
    },
    *saveCutterCamCheckThres({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'ph',
        'web_request_save_cutter_cam_check_thres',
        [payload]
      );
      if (response === 'ok') {
        message.success('保存成功!');
      } else {
        message.error('保存失败,' + response, 5);
      }
      yield put({
        type: 'getCutterCamCheckThres',
      });
    },
  },
  reducers: {
    updateOfflineZmcParams(state, action) {
      return {
        ...state,
        offlineZmcParams: action.payload,
      };
    },
    updateCamZmcParams(state, action) {
      return {
        ...state,
        camZmcParams: action.payload,
      };
    },
    updateCbjZmcParams(state, action) {
      return {
        ...state,
        cbjZmcParams: action.payload,
      };
    },
    updateCutterZmcParams(state, action) {
      return {
        ...state,
        cutterZmcParams: action.payload,
      };
    },
    updateLineCamCheckThres(state, action) {
      return {
        ...state,
        lineCamCheckThres: action.payload,
      };
    },
    updateCutterCamCheckThres(state, action) {
      return {
        ...state,
        cutterCamCheckThres: action.payload,
      };
    },
  },
};
